package ldh.text.util;

import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class DialogUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(DialogUtil.class);

    public static <T> Optional<T> show(String title, Consumer<JFXDialogLayout> dialogLayoutConsumer, double width, double height) {
        JFXAlert<T> jfxAlert = new JFXAlert<>();
        JFXDialogLayout dialogLayout = new JFXDialogLayout();
        dialogLayout.setHeading(new Label(title));
        TextField nameTextField = new TextField();
        dialogLayout.setBody(nameTextField);
        JFXButton submitBtn = submitBtn(jfxAlert, ja-> ja.setResult(nameTextField.getText()));
        dialogLayout.setActions(submitBtn, closeBtn(jfxAlert));
        dialogLayout.setPrefSize(width, height);
        if (dialogLayoutConsumer != null) {
            dialogLayoutConsumer.accept(dialogLayout);
        }
        jfxAlert.setContent(dialogLayout);
        jfxAlert.setOnCloseRequest(e->{
            jfxAlert.setResult((T) nameTextField.getText());
        });
        return jfxAlert.showAndWait();
    }

    public static void showMessage(String title, String text, double width, double heigth) {
        showDialog(title, (jfxDialog, jfxDialogLayout) -> {
            Text text1 = new  Text(text);
            jfxDialogLayout.setBody(text1);
        }, width, heigth);
    }

    public static void showDialog(String title, BiConsumer<JFXDialog, JFXDialogLayout> jfxDialogConsumer, double width, double heigth) {
        showDialog(title, jfxDialogConsumer, width, heigth, false);
    }

    public static void showDialog(String title, BiConsumer<JFXDialog, JFXDialogLayout> jfxDialogConsumer, double width, double heigth, boolean isModel) {
        JFXDialog dialog = new JFXDialog();
        JFXDialogLayout dialogLayout = new JFXDialogLayout();
        dialogLayout.setHeading(new Label(title));
        dialogLayout.setActions(closeBtn(dialog));
        dialogLayout.setPrefSize(width, heigth);
        dialog.setTransitionType(JFXDialog.DialogTransition.CENTER);
        dialog.setContent(dialogLayout);
        dialog.setOverlayClose(!isModel);
        if (jfxDialogConsumer != null) jfxDialogConsumer.accept(dialog, dialogLayout);
        dialog.show(RegionUtil.getRootPane());
        LOGGER.info("dialog children size: " + RegionUtil.getRootPane().getChildren().size());
    }

    private static JFXButton closeBtn(JFXDialog jfxDialog) {
        JFXButton jfxButton = new JFXButton("关闭");
        jfxButton.setOnAction(e-> jfxDialog.close());
        return jfxButton;
    }

    private static JFXButton closeBtn(JFXAlert jfxDialog) {
        JFXButton jfxButton = new JFXButton("关闭");
        jfxButton.setOnAction(e-> jfxDialog.close());
        return jfxButton;
    }

    private static JFXButton submitBtn(JFXAlert jfxDialog, Consumer<JFXAlert> consumer) {
        JFXButton jfxButton = new JFXButton("确认");
        jfxButton.setOnAction(e-> {
            if (consumer != null) {
                consumer.accept(jfxDialog);
            }
            jfxDialog.close();
        });
        return jfxButton;
    }
}
